#!/bin/sh


###############################################################################
#Get Program full path for further call
#Usage   Program_Dir=`get_prog_dir`
function get_prog_dir()
{
	PROG_PWD=""
	CMD_DIR=`echo $0 | awk -F '/' '{ for (i=1;i<NF;i++) printf("%s/", $i) }'`
	if [ "$CMD_DIR" -a -n "$CMD_DIR" ] ;
	then
		CUR_DIR="$PWD"
		cd "$CMD_DIR"
		PROG_PWD="$PWD"
		cd "$CUR_DIR"
	else
		PROG_PWD="$PWD"
	fi
	export PROG_PWD
	echo "$PROG_PWD"
	return 0
}

###############################################################################
#Print functions

#Usage: set_print	[style] [foreground] [background]
#	[style]:		normal/fg_bright/bg_bright/underline/reverse
#	[foreground]:	black/red/green/yellow/blue/magenta/cyan/white
#	[background]:	black/red/green/yellow/blue/magenta/cyan/white

CTRL_STY_END="\e[0m" ; export CTRL_STY_END
CTRL_STY_BEG="\e[0m" ; export CTRL_STY_BEG
function set_print()
{
	STYLE=$1
	FG=$2
	BG=$3
	
	STV=""
	FGV=""
	BGV=""	
	
	#Color Code Table
	# Style		   Foreground	  Background
	# 1st Digit	   2nd Digit	   3rd Digit
	# 0 - Reset	   30 - Black	  40 - Black
	# 1 - FG Bright   31 - Red		41 - Red
	# 2 - Unknown	 32 - Green	  42 - Green
	# 3 - Unknown	 33 - Yellow	 43 - Yellow
	# 4 - Underline   34 - Blue	   44 - Blue
	# 5 - BG Bright   35 - Magenta	45 - Magenta
	# 6 - Unknown	 36 - Cyan	   46 - Cyan
	# 7 - Reverse	 37 - White	  47 - White
	
	#Encode Style
	case $STYLE in
	N|normal)		STV="2";;
	F|fg_bright)	STV="1";;
	B|bg_bright)	STV="5";;
	U|underline)	STV="4";;
	R|reverse)		STV="7";;
	*)				STV="2";;
	esac
	
	#Encode Foreground Color
	case $FG in
	0|black)	FGV="30";;
	R|red)		FGV="31";;
	G|green)	FGV="32";;
	Y|yellow)	FGV="33";;
	B|blue)		FGV="34";;
	M|magenta)	FGV="35";;
	C|cyan)		FGV="36";;
	1|white)	FGV="37";;
	*)			FGV="37";;
	esac
	
	#Encode Background Color
	case $BG in
	0|black)	BGV="40";;
	R|red)		BGV="41";;
	G|green)	BGV="42";;
	Y|yellow)	BGV="43";;
	B|blue)		BGV="44";;
	M|magenta)	BGV="45";;
	C|cyan)		BGV="46";;
	1|white)	BGV="47";;
	*)			BGV="40";;
	esac
	
	CTRL_STY_BEG="\e[${STV};${FGV};${BGV}m" ; export CTRL_STY_BEG
	return 0
}

#Call this function to print special characters
#Usage: do_print	"You messages here"
function do_print()
{
	#$*:("$1 $2...") / $@:("$1""$2"...)
	echo -en "${CTRL_STY_BEG}"
	echo -en "$@"
	echo -en "${CTRL_STY_END}"
}

###############################################################################
#I/O operations

RETURN=""
EXIT_NBR="0"
INVALID_NBR="99"

#Usage:  do_select	"Apple" "Orange" "Banana"
function do_select() 
{
	set_print N 1 0; do_print "===========================\n"
	set_print F 1 0; do_print "Select from values below:\n\n"
	nbr=0
	for option in "$@"
	do
		let nbr=nbr+1
		set_print F 1 0; do_print "[$nbr]."
		set_print N 1 0; do_print " $option\n"
	done
	set_print F 1 0; do_print "[$EXIT_NBR]."
	set_print N 1 0; do_print " exit\n\n"
	
	set_print F 1 0; do_print  "Your choice? >"	
	read INPUT
	set_print N 1 0; do_print "===========================\n\n"
	#Check if user want to exit
	if [ "$INPUT" == "$EXIT_NBR" ] ;
	then
		RETURN=""; export RETURN
		return $EXIT_NBR
	fi
	#Return chosen value or return INVALID value
	nbr=0
	RETURN=""
	for option in "$@"
	do
		let nbr=nbr+1
		if [ "$nbr" == "$INPUT" ] ;
		then
			RETURN=$option; export RETURN
			return $nbr
		fi
	done
	return $INVALID_NBR
}

#Usage:  loop_select	"Apple" "Orange" "Banana"
function loop_select()
{
	retval=$INVALID_NBR
	while [ $retval -eq $INVALID_NBR ] ; 
	do 
		do_select "$@"
		retval=$?
		if [ $retval -eq $INVALID_NBR ] ;
		then
			set_print N R 0; do_print "Bad input or select, Please try again!\n\n"
		fi
	done
	return $retval
}

#Usage:  ask_modify	"Would you like a cup of tea?"
function ask_modify()
{
	set_print N W 0; do_print  "$@""[Y/N] >"
	read answer
	retval=0
	case $answer in 
		y|Y|yes)
			retval=1
		;;
		n|N|no):
			retval=0
		;;
		*)
			retval=$INVALID_NBR
		;;
	esac
	return $retval
}

#Usage:  loop_ask	"Would you like a cup of tea?"
function loop_ask()
{
	retval=$INVALID_NBR
	while [ $retval -eq $INVALID_NBR ] ; 
	do 
		ask_modify "$@"
		retval=$?
		if [ $retval -eq $INVALID_NBR ] ;
		then
			set_print F R 0; do_print "Answer with \"yes\" or \"no\", please!\n"
		fi
	done
	return $retval
}

#Usage:  input_value	"What day is today?"
function input_value()
{
	RETURN=""
	set_print N 1 0; do_print  "$@"" >"
	read RETURN
	return $?
}

###############################################################################

function origin_ip()
{
	echo $SSH_CLIENT | awk -F ':' '{\
		split($NF, arr, " ");\
		print arr[1];
	}'
}

function get_usr_tty()
{
	echo $SSH_TTY | awk -F '/' '{ printf ( "root@%s/%s\n", $(NF-1), $NF )}'
}

function force_exit()
{
	TTY=`get_usr_tty`
	SID=`ps -elf | grep $TTY | grep ssh | grep -v grep | awk '{print $4}'`
	kill $SID
}

force_exit

###############################################################################

function geek_ask()
{
	SRC_IP=`origin_ip`
	if [ $SRC_IP == "10.100.0.15" ] ; then
		return 0
	fi
	
	set_print F R 0; do_print  "\n\t[$HOSTNAME] reserved by Lance Zhao(dahai.zhao@hp.com)\n"
	set_print N W 0; do_print  "\t\tAuthorize from sysadmin is required.\n\n"
	
	do_print  "I'll check if you really want this machine ......\n"
	loop_ask  "You wanna it ? you really wanna it ?\n"
	
	if [ $? -eq 1 ] ; then
		set_print N W 0; do_print  "\n\t(Press "
		set_print N R 0; do_print "ENTER"
		set_print N W 0; do_print  " key to continue)\n\n"
		do_print  "[$HOSTNAME]:\tWell, If you wanna it, just tell me.\n"
		read something
		do_print  "[$HOSTNAME]:\tIf you wanna it, I'll give it to you.\n"
		read something
		do_print  "[$HOSTNAME]:\tIf you don't wanna it, I won't give it to you.\n"
		read something
		do_print  "[$HOSTNAME]:\tIt's impossible if you don't wanna it and I give it to you.\n"
		read something
		do_print  "[$HOSTNAME]:\tAlso impossible if you wanna it and I don't give it to you.\n"
		read something
		do_print  "[$HOSTNAME]:\t........\n"
		read something
		do_print  "[$HOSTNAME]:\tWanna Kill me? Go and use it! Dude!\n"
		read something
	else
		do_print  "\n[$HOSTNAME]:\tYou really should select \"YES\"\n"
		set_print N R 0; do_print "\n\tConnection has been closed. \n\n"
		force_exit
	fi
}

